Warn about unused keys in dependency specification
authorAleksey Kladov <aleksey.kladov@gmail.com>
Mon, 1 Aug 2016 13:36:29 +0000 (16:36 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Mon, 1 Aug 2016 13:36:29 +0000 (16:36 +0300)
src/cargo/util/toml.rs
tests/bad-config.rs

index 158fbdc225b00830dbcce605c3155d8c7799c578..7de06ddc250639f822c215ca91681f88dff398e9 100644 (file)
@@ -775,8 +775,44 @@ impl TomlDependency {
             cx.warnings.push(msg);
         }
 
+        if details.git.is_none() {
+            let git_only_keys = [
+                (&details.branch, "branch"),
+                (&details.tag, "tag"),
+                (&details.rev, "rev")
+            ];
+
+            for &(key, key_name) in git_only_keys.iter() {
+                if key.is_some() {
+                    let msg = format!("key `{}` is ignored for dependency ({}). \
+                                       This will be considered an error in future versions",
+                                      key_name, name);
+                    cx.warnings.push(msg)
+                }
+            }
+        }
+
         let new_source_id = match (details.git.as_ref(), details.path.as_ref()) {
-            (Some(git), _) => {
+            (Some(git), maybe_path) => {
+                if maybe_path.is_some() {
+                    let msg = format!("dependency ({}) specification is ambiguous. \
+                                       Only one of `git` or `path` is allowed. \
+                                       This will be considered an error in future versions", name);
+                    cx.warnings.push(msg)
+                }
+
+                let n_details = [&details.branch, &details.tag, &details.rev]
+                    .iter()
+                    .filter(|d| d.is_some())
+                    .count();
+
+                if n_details > 1 {
+                    let msg = format!("dependency ({}) specification is ambiguous. \
+                                       Only one of `branch`, `tag` or `rev` is allowed. \
+                                       This will be considered an error in future versions", name);
+                    cx.warnings.push(msg)
+                }
+
                 let reference = details.branch.clone().map(GitReference::Branch)
                     .or_else(|| details.tag.clone().map(GitReference::Tag))
                     .or_else(|| details.rev.clone().map(GitReference::Rev))
index 997fb30a5cf14b5ee034b93f2ecb8893a33380ef..2ccece7d550f73c15b2a4ba0d0701bc7fbe108d1 100644 (file)
@@ -513,3 +513,71 @@ in the future.
 [FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
 "));
 }
+
+#[test]
+fn ambiguous_git_reference() {
+    let foo = project("foo")
+    .file("Cargo.toml", r#"
+        [package]
+        name = "foo"
+        version = "0.0.0"
+        authors = []
+
+        [dependencies.bar]
+        git = "https://example.com"
+        branch = "master"
+        tag = "some-tag"
+    "#)
+    .file("src/lib.rs", "");
+
+    assert_that(foo.cargo_process("build").arg("-v"),
+                execs().with_stderr_contains("\
+[WARNING] dependency (bar) specification is ambiguous. \
+Only one of `branch`, `tag` or `rev` is allowed. \
+This will be considered an error in future versions
+"));
+}
+
+#[test]
+fn both_git_and_path_specified() {
+    let foo = project("foo")
+        .file("Cargo.toml", r#"
+        [package]
+        name = "foo"
+        version = "0.0.0"
+        authors = []
+
+        [dependencies.bar]
+        git = "https://example.com"
+        path = "bar"
+    "#)
+        .file("src/lib.rs", "");
+
+    assert_that(foo.cargo_process("build").arg("-v"),
+                execs().with_stderr_contains("\
+[WARNING] dependency (bar) specification is ambiguous. \
+Only one of `git` or `path` is allowed. \
+This will be considered an error in future versions
+"));
+}
+
+#[test]
+fn ignored_git_revision() {
+    let foo = project("foo")
+        .file("Cargo.toml", r#"
+        [package]
+        name = "foo"
+        version = "0.0.0"
+        authors = []
+
+        [dependencies.bar]
+        path = "bar"
+        branch = "spam"
+    "#)
+        .file("src/lib.rs", "");
+
+    assert_that(foo.cargo_process("build").arg("-v"),
+                execs().with_stderr_contains("\
+[WARNING] key `branch` is ignored for dependency (bar). \
+This will be considered an error in future versions"));
+}